package com.hmall.user.service.impl;

import com.hmall.user.mapper.AddressMapper;
import com.hmall.user.pojo.Address;
import com.hmall.user.service.AddressService;
import com.hmall.utils.AssertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mengjie
 */
@Slf4j
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void addAddress(Address address) {
        // 校验省份/城市/区县
        AssertUtils.assertNotNull(address.getContact(), "收货人不能为空");
        AssertUtils.assertNotNull(address.getMobile(), "手机号不能为空");
        AssertUtils.assertNotNull(address.getProvince(), "省份不能为空");
        AssertUtils.assertNotNull(address.getCity(), "城市不能为空");
        AssertUtils.assertNotNull(address.getTown(), "区县不能为空");
        AssertUtils.assertNotNull(address.getStreet(), "详细地址不能为空");
        addressMapper.addAddress(address);
    }

    @Override
    public void deleteAddress(Long id) {
        addressMapper.deleteById(id);
    }

    @Override
    public void updateAddress(Address address) {
        addressMapper.update(address);
    }

    @Override
    public List<Address> queryByUserId(Long userId) {
        return addressMapper.queryByUserId(userId);
    }

    @Override
    public Address getById(Long id) {
        return addressMapper.getById(id);
    }
}
